package view.helper;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Window;

import view.component.EndCellComponent;
import view.component.FreeCellComponent;
import view.component.PileComponent;



/**
 * @author  sangwo.y
 */
public class Setup {
	
	public static final int COLUMN_X_OFFSET = 15;
	public static final int COLUMN_Y_OFFSET = 200;
	public static final int COLUMN_X_STEP = 15;
	public static final int COLUMN_Y_STEP = 25;
	
	public static final int CELL_X_OFFSET = 15;	
	public static final int CELL_Y_OFFSET = 50;
	public static final int CELL_X_STEP = 5;
	
	public static final int CELL_X_SPACING = 70;
	
	public static final int CARD_WIDTH = 92;
	public static final int CARD_HEIGHT = 133;
			
	Window zulGame;
	Adaptor adaptor;

	public Setup(Component zulGame) {			
		this.zulGame = (Window) zulGame; 
	}
	
	public void newGame() {
		adaptor = (Adaptor) zulGame.getAttribute("model");
		populateWindow();
	}
	
	private void populateWindow() {

		Integer pLeft = COLUMN_X_OFFSET, pTop = COLUMN_Y_OFFSET;
		
		try {	
			/*draw pile holders */
			PileComponent[] piles = adaptor.getPiles();
			for (int i=0; i<piles.length; i++) {
				PileComponent pile = piles[i];
				pile.setStep(COLUMN_Y_STEP);
				pile.drawAtPosition(pTop, pLeft);				
				
				pLeft = pLeft + CARD_WIDTH + COLUMN_X_STEP;
			}
			
			/*draw freecell holders*/
			pTop = CELL_Y_OFFSET;
			pLeft = CELL_X_OFFSET;
			
			FreeCellComponent[] freecells = adaptor.getFreeCells();
			for (int i=0; i<freecells.length; i++) {
				FreeCellComponent freecell = freecells[i];
				freecell.drawAtPosition(pTop, pLeft);
				
				pLeft = pLeft + CARD_WIDTH + CELL_X_STEP;
			}			
			
			/*draw endcell holders */
			pTop = CELL_Y_OFFSET;
			pLeft = pLeft + CELL_X_SPACING;			
			
			EndCellComponent[] endcells = adaptor.getEndCells();
			for (int i=0; i<endcells.length; i++) {
				EndCellComponent endcell = endcells[i];
				endcell.drawAtPosition(pTop, pLeft);
				
				pLeft = pLeft + CARD_WIDTH + CELL_X_STEP;
			}	
					
			adaptor.show(zulGame);
					
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
